<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * This is the key class to all the admin side data nodes.
    * It is extended into child classes which do all the work.
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 25.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class adminNodePage
    ****
    * This class handles the editing of simple HTML pages in INA
    ****
    * @package adminNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 25.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class adminNodeShopArticlePackage extends adminNode{
        
		/**
         * a data processor.
         * 
         * @return 
         **/
        function process(){
            if($_POST){
                if($_POST['on_splash'] == 'TRUE'){
                    $onSplash = 'TRUE';
                }else{
                    $onSplash = 'FALSE';
                }
                
                //upload picture (if any)
                if($_POST['img'] or $this->uploadPicture()){
                    $img = "TRUE";
                }else{
                    $img = "FALSE";
                }
                
                //update database record
                $this->db->Execute("UPDATE node_shop_package SET image = $img, content = ".$this->db->qstr($_POST['content']).", teaser = ".$this->db->qstr($_POST['teaser']).", on_splash = ".$onSplash." WHERE id = " . $this->object);
                
                //updates the status of the page (title, wf state)
                $this->updatePageStatus();
                
                //make a note of the ajax request
                $this->log->recordAction("Updated Shop Article Package (".$this->object.") with global id (".$this->pageref.")");

                header('Location: '.$this->request.'&confirm=true');
                exit;
            }else{
                $this->smarty->assign('page',$this->db->getRow("SELECT node_shop_package.image, title, content, teaser, on_splash, pagemap.pageid AS pid FROM pagemap LEFT JOIN node_shop_package ON pagemap.pageid = node_shop_package.id WHERE pagemap.id = " . $this->pageref));
                
                $pType   = $this->db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticle'");
                $present = $this->db->getCol("SELECT 
                                                article_id 
                                                FROM node_shop_package_article a LEFT JOIN pagemap p ON a.article_id = p.pageid AND p.pagetype = ".(int)$pType." 
                                                WHERE package_id = ".$this->object."
                                                ORDER BY title"); $present[] = 0;
                $this->smarty->assign('articles',$this->db->getAssoc("SELECT pageid, title FROM pagemap WHERE pagetype = ".(int)$pType." AND pageid NOT IN(".implode(',',$present).") ORDER BY title"));
                $this->smarty->assign('inpack',$this->db->getAll("SELECT article_id, title FROM node_shop_package_article a LEFT JOIN pagemap p ON a.article_id = p.pageid AND p.pagetype = ".(int)$pType." WHERE package_id = ".$this->object." ORDER BY title"));
            }
        }
        
        /**
         * Will set the HTML template needed for
         * the child's operation
         * 
         * @return 
         **/
        function setTemplate(){
            $this->setActionTemplate('nodes/editShopArticlePackage.html');
        }
        
        
		/** 
         * method which will handle a default add action to the
         * dedicated table
         * 
         * @return integer ID of new object
         * @see ajaxAddPage
         * @see ajaxAddNode
         **/
        function addInstance(){
            $this->db->Execute("INSERT INTO node_shop_package(content) VALUES('')");
            return $this->db->getOne("SELECT last_value FROM node_shop_package_id_seq");
        }
    }
?>